#!/var/bin/python
# -*- coding: utf-8 -*-
#######################
#	获取企查查企业基础信息
#######################

import sys
import time
import urllib
import re
from bs4 import BeautifulSoup
import io
import os
dirs = os.path.abspath(os.path.dirname(__file__)+"/../Config")
os.sys.path.append(dirs)   #将上上级目录加载到python的环境变量中
# os.sys.path.append("D:/job/crawler/Config")
from config import conn,webdriver,logpath
from selenium.webdriver.common.keys import Keys  #selenium 输入需要引入keys包
import socket

ip = socket.gethostbyname(socket.gethostname())



#获取详请页面内容 处理
def dealData(searchname):
	phantomjspath = "D:/phantomjs%s/bin/phantomjs" % 0
	# driver = webdriver.Firefox()
	driver = webdriver.PhantomJS(executable_path=phantomjspath)
	driver.implicitly_wait(60)

	print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),'Start crawling the data. . .')

	cur = conn.cursor()
	print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"Open your browser. . .")
	#打开登陆页面
	print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"Open the login page. . .")
	# soup = DriverGet("https://graph.qq.com/oauth/show?which=Login&display=pc&response_type=code&client_id=101188807&redirect_uri=http%3A%2F%2Fwww.qichacha.com%2Fuser_callbackqq&state=3daff0f5935d33e95facd2033cdbb312&scope=get_user_info,add_share","combine_page",eid=eid,enterprise=searchname)
	soup = driver.get("https://graph.qq.com/oauth/show?which=Login&display=pc&response_type=code&client_id=101188807&redirect_uri=http%3A%2F%2Fwww.qichacha.com%2Fuser_callbackqq&state=3daff0f5935d33e95facd2033cdbb312&scope=get_user_info,add_share")
	driver.implicitly_wait(60)
	# driver.set_page_load_timeout(30)
	# driver.set_script_timeout(30)
	driver.switch_to_frame('ptlogin_iframe')

	print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"Switch to the use of account password login. . .")
	driver.find_element_by_id('switcher_plogin').click()
	print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"Enter QQ account password. . .")
	driver.find_element_by_id("u").clear()
	driver.find_element_by_id("u").send_keys("2252875088")
	driver.find_element_by_id("p").clear()
	driver.find_element_by_id("p").send_keys("123698745")
	driver.find_element_by_id("login_button").click()
	time.sleep(3)
	the_page = BeautifulSoup(driver.page_source,"html.parser")
	pos = str(the_page).find("15042526")
	if pos < 0:
		driver.close()
		driver.quit()
		return dealData(searchname)
		
	print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"Search in the home page. . .")
	driver.find_element_by_id("searchkey").clear()
	driver.find_element_by_id("searchkey").send_keys(searchname)
	driver.find_element_by_id("searchkey").send_keys(Keys.ENTER)
	# driver.get("http://www.qichacha.com/search?key=%s" % searchname)
	time.sleep(3)
	if driver.find_element_by_id("searchlist"):
		print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"Open the Company page. . .")
		driver.find_element_by_id("searchlist").find_element_by_tag_name("a").click()
		time.sleep(3)
		handles = driver.window_handles # 获取当前窗口句柄集合（列表类型）
		driver.switch_to_window(handles[-1])
		soup = BeautifulSoup(driver.page_source,"html.parser")
		base_lis = soup.find("section",id="Cominfo").find_all("td",class_="ma_left")

		base = {}
		baseinfolist = []
		columnname = []
		columnval = []
		for base_li in base_lis:
			if base_lis.index(base_li) % 2 == 0:
				columnname.append(base_li)
			else:
				columnval.append(base_li)
		for j in range(len(columnname)):
			baseinfolist.append(columnname[j].text.strip()+columnval[j].text.strip())
		for columninfo in baseinfolist:
			base_liarr = columninfo.split("：")
			base[base_liarr[0].strip()] = base_liarr[1].strip()

		fullname = soup.find("span",class_="text-big").text.strip()
		#判断是否已经存在数据
		sqls = "select * from enterpriseinfo where ename like '%s'" % ("%"+fullname+"%")
		cur.execute(sqls)
		result = cur.fetchone()
		print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"data processing. . .")
		if result:
			sql = "update enterpriseinfo set `searchname`='%s',`ename`='%s',`creditcode`='%s',`regnumber`='%s',`organizationcode`='%s',`managementforms`='%s',`companytype`='%s',`RegisterDate`='%s',`legalperson`='%s',`registeredcapital`='%s',`busnissallotedtime`='%s',`registeroffice`='%s',`dateofissue`='%s',`scale`='%s',`industry`='%s',`Englishname`='%s',`businessaddress`='%s',`Businessscope`='%s',`status`=1 where eid=%d" % (searchname,fullname,base.get("统一社会信用代码",""),base.get("注册号",""),base.get("组织机构代码",""),base.get("经营状态",""),base.get("公司类型",""),base.get("成立日期",""),base.get("法定代表人",""),base.get("注册资本",""),base.get("营业期限",""),base.get("登记机关",""),base.get("发照日期",""),base.get("公司规模",""),base.get("所属行业",""),base.get("英文名",""),base.get("企业地址",""),base.get("经营范围",""),result[0])
		else:
			sql = "insert into enterpriseinfo (`searchname`,`ename`,`creditcode`,`regnumber`,`organizationcode`,`managementforms`,`companytype`,`RegisterDate`,`legalperson`,`registeredcapital`,`busnissallotedtime`,`registeroffice`,`dateofissue`,`scale`,`industry`,`Englishname`,`businessaddress`,`Businessscope`,`status`) values ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s',1)" % (searchname,fullname,base.get("统一社会信用代码",""),base.get("注册号",""),base.get("组织机构代码",""),base.get("经营状态",""),base.get("公司类型",""),base.get("成立日期",""),base.get("法定代表人",""),base.get("注册资本",""),base.get("营业期限",""),base.get("登记机关",""),base.get("发照日期",""),base.get("公司规模",""),base.get("所属行业",""),base.get("英文名",""),base.get("企业地址",""),base.get("经营范围",""))
			
		print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"Add company basic data to the database")
		exestatus = "[SUCCESS]:"
		try:
			cur.execute(sql)
			conn.commit()
		except Exception as e:
			print("Failed to add data...")
			exestatus = "[ERROR]:"

		# file_object = open(logpath+"/enterprise.log", 'a')
		# file_object.write(exestatus+"	"+time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+"	"+sql+"\r\n")
		# file_object.close()
		#########数据处理完
	else:
		print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"data not found...")

print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"Start crawling company basic data. . .")
#企业列表
if "php" in sys.argv:
	enterprise = urllib.request.unquote(sys.argv[1])
else:
	enterprise = sys.argv[1]
dealData(enterprise)
# driver.close()
# driver.quit()
